﻿#
# Manage Business Object service & components
#

#Stop the BO Server with tow modes Gracefully or force
function Stop-BOServer
{
	[CmdletBinding()]
		param (
			[parameter(Mandatory=$false)]
			[switch]$force
			)
    PROCESS 
	{
		#Read services related to BO
        Stop-Tomcat -Verbose:($PSBoundParameters['Verbose'] -eq $true)
        Stop-SIA -force:$force -Verbose:($PSBoundParameters['Verbose'] -eq $true)
        Stop-Apache -Verbose:($PSBoundParameters['Verbose'] -eq $true)
    }
	END
	{
        #Re-read services to have the final status
		$svcSIA = Get-Service | Where-Object -FilterScript {$_.Name -like 'BOE120SIA*'}
		$svcTomcat = Get-Service | Where-Object -FilterScript {$_.Name -like 'BOE120Tomcat'}
		$svcApache = Get-Service | Where-Object -FilterScript {$_.Name -like 'Apache2.2'}
        
        $SiaStatus=$false
        $TomcatStatus=$false
        $ApacheStatus=$false

		if ($svcSIA -ne $null)
		{
			$SiaStatus = $svcSIA.Status
		}

		if ($svcTomcat -ne $null)
		{
			$TomcatStatus = $svcTomcat.Status
		}

		if ($svcApache -ne $null)
		{
			$ApacheStatus = $svcApache.Status
		}
        

		$FinalStatus = [System.String]::Format("Stop BO Server (Apache, Tomcat, SIA) : {0}", ($ApacheStatus -eq 'Stopped') -and ($TomcatStatus -eq 'Stopped') -and ($SiaStatus -eq 'Stopped'))
		write-Output $FinalStatus
        Write-Verbose "End stop BO Server"
	}
    BEGIN
    {
        Write-Verbose "Begin stop BO Server"
    }
}

#Start the BO Server with tow modes Gracefully or force
function Start-BOServer
{
	[CmdletBinding()]
		param()
    PROCESS 
	{
		#Read services related to BO
        Start-Tomcat -Verbose:($PSBoundParameters['Verbose'] -eq $true)
        Start-SIA -Verbose:($PSBoundParameters['Verbose'] -eq $true)
        Start-Apache -Verbose:($PSBoundParameters['Verbose'] -eq $true)
    }
	END
	{
        #Re-read services to have the final status
		$svcSIA = Get-Service | Where-Object -FilterScript {$_.Name -like 'BOE120SIA*'}
		$svcTomcat = Get-Service | Where-Object -FilterScript {$_.Name -like 'BOE120Tomcat'}
		$svcApache = Get-Service | Where-Object -FilterScript {$_.Name -like 'Apache2.2'}
        
        $SiaStatus=$false
        $TomcatStatus=$false
        $ApacheStatus=$false

		if ($svcSIA -ne $null)
		{
			$SiaStatus = $svcSIA.Status
		}

		if ($svcTomcat -ne $null)
		{
			$TomcatStatus = $svcTomcat.Status
		}

		if ($svcApache -ne $null)
		{
			$ApacheStatus = $svcApache.Status
		}
        

		$FinalStatus = [System.String]::Format("Start BO Server (Apache, Tomcat, SIA) : {0}", ($ApacheStatus -eq 'Running') -and ($TomcatStatus -eq 'Running') -and ($SiaStatus -eq 'Running'))
		write-Output $FinalStatus
        Write-Verbose "End start BO Server"
	}
    BEGIN
    {
        Write-Verbose "Begin start BO Server"
    }
}

#Stop Server Intelligence Agent
function Stop-SIA
{
    [CmdletBinding()]
        param (
			[parameter(Mandatory=$false)]
			[switch]$force
        )
    PROCESS 
	{
		if ($force -ne $true)
		{
			write-Verbose "Mode gracefully"
            $localCms = $env:COMPUTERNAME + ":6400"
            
            #first stop Event Server
            $boServer = [System.String]::Format("{0}.EVENTSERVER",$env:COMPUTERNAME)
			write-Verbose "Stop $boServer"
            
            
		}
        Stop-BOService -ServicePattern 'BOE120SIA*' -ServiceFriendlyName SIA -Verbose:($PSBoundParameters['Verbose'] -eq $true)
    }
}

#Start Server Intelligence Agent
function Start-SIA
{
    [CmdletBinding()]
        param ()
    PROCESS 
	{
        Start-BOService -ServicePattern 'BOE120SIA*' -ServiceFriendlyName SIA -Verbose:($PSBoundParameters['Verbose'] -eq $true)
    }
}

#Stop Apache service
function Stop-Apache
{
    [CmdletBinding()]
        param ()
    PROCESS 
	{
        Stop-BOService -ServicePattern 'Apache2.2' -ServiceFriendlyName "Apache 2.2" -Verbose:($PSBoundParameters['Verbose'] -eq $true)
    }
}

#Start Apache Service
function Start-Apache
{
    [CmdletBinding()]
        param ()
    PROCESS 
	{
        Start-BOService -ServicePattern 'Apache2.2' -ServiceFriendlyName "Apache 2.2" -Verbose:($PSBoundParameters['Verbose'] -eq $true)
    }
}

#Stop Tomcat Service
function Stop-Tomcat
{
    [CmdletBinding()]
        param ()
    PROCESS 
	{
        Stop-BOService -ServicePattern 'BOE120Tomcat' -ServiceFriendlyName Tomcat -Verbose:($PSBoundParameters['Verbose'] -eq $true)
    }
}

#Start Tomcat Service
function Start-Tomcat
{
    [CmdletBinding()]
        param ()
    PROCESS 
	{
        Start-BOService -ServicePattern 'BOE120Tomcat' -ServiceFriendlyName Tomcat -Verbose:($PSBoundParameters['Verbose'] -eq $true)
    }
}


#Managed Stop a BO Service
function Stop-BOService
{
    [CmdletBinding()]
        param (
			[parameter(Mandatory=$true)]
			[String]$ServicePattern,
            
			[parameter(Mandatory=$true)]
			[String]$ServiceFriendlyName
            
			)
    PROCESS 
	{
		#Read services related to BO
		$svc = Get-Service | Where-Object -FilterScript {$_.Name -like $ServicePattern}

		#Check Services
		if ($svc -eq $null)
		{
             $msg = [System.String]::Format("Service {0} not found with this pattern {1}", $ServiceFriendlyName,$ServicePattern)
			 write-Error $msg
			 return
		}
        
		if ($svc.Status -ne 'Stopped')
		{

            $msg = [System.String]::Format("Stop Windows Service {0}", $svc.DisplayName)
			write-Verbose $msg
			Stop-Service -InputObject $svc
		}
		else
		{
            $msg = [System.String]::Format("Windows Service {0} is already stopped", $svc.DisplayName)
			write-Verbose $msg
		}

    }
    BEGIN
    {
        $msg = [System.String]::Format("Begin stop BO Service {0}", $ServiceFriendlyName)
        Write-Verbose $msg
   }
	END
	{
		if ($svc -ne $null)
		{
			$svcStatus = [System.String]::Format("Service {0} : {1}", $svc.Name, $svc.Status)
			write-Output $svcStatus
		}


		$FinalStatus = [System.String]::Format("Stop {0} : {1}", $svc.Name, ($svc.Status -eq 'Stopped'))
		write-Output $FinalStatus


        $msg = [System.String]::Format("End stop BO Service {0}", $ServiceFriendlyName)
        Write-Verbose $msg
	}
}

#Managed Start a BO Service
function Start-BOService
{
    [CmdletBinding()]
        param (
			[parameter(Mandatory=$true)]
			[String]$ServicePattern,
            
			[parameter(Mandatory=$true)]
			[String]$ServiceFriendlyName
            
			)
    PROCESS 
	{
		#Read services related to BO
		$svc = Get-Service | Where-Object -FilterScript {$_.Name -like $ServicePattern}

		#Check Services
		if ($svc -eq $null)
		{
             $msg = [System.String]::Format("Service {0} not found with this pattern {1}", $ServiceFriendlyName,$ServicePattern)
			 write-Error $msg
			 return
		}
        
		if ($svc.Status -ne 'Running')
		{

            $msg = [System.String]::Format("Start Windows Service {0}", $svc.DisplayName)
			write-Verbose $msg
			Start-Service -InputObject $svc
		}
		else
		{
            $msg = [System.String]::Format("Windows Service {0} is already Started", $svc.DisplayName)
			write-Verbose $msg
		}

    }
    BEGIN
    {
        $msg = [System.String]::Format("Begin start BO Service {0}", $ServiceFriendlyName)
        Write-Verbose $msg
    }
	END
	{
		if ($svc -ne $null)
		{
			$svcStatus = [System.String]::Format("Service {0} : {1}", $svc.Name, $svc.Status)
			write-Output $svcStatus
		}


		$FinalStatus = [System.String]::Format("Start {0} : {1}", $svc.Name, ($svc.Status -eq 'Running'))
		write-Output $FinalStatus

        $msg = [System.String]::Format("End start BO Service {0}", $ServiceFriendlyName)
        Write-Verbose $msg

	}
}
